﻿function Helper() {

    this.GetOrderAll = function (func) {
        AysPost("GetOrderAll",
              { UserName: userName },
            func);
    };

    this.GetOrderDetails = function (OrderId, func) {
        AysPost("GetOrderAll",
              { orderId: OrderId },
            func);
    };

    this.GetAllRoles = function (func) {
        AysPost("getAllRole",
              { },
            func);
    };

    this.GetAllUsers = function (func) {
        AysPost("getAllUser",
              {  },
            func);
    };

    this.Login = function (UserName, Password, func) {
        AysPost("login",
              {
                  username: UserName,
                  password: Password
              },
            func);
    };

    this.SaveDocument = function (Documents,DocumentDetails, func) {
        AysPost("UpdateDocument",
              {
                  documents: Documents,
                  documentDetails : DocumentDetails
              },
            func);
    };

    this.SaveComment = function (DocumentDetails, func) {
        AysPost("SaveComment",
              {
                  savecomment: DocumentDetails
              },
            func);
    };

    this.GetATP = function (ProdIds, func) {
        AysPost("GetATP",
              {
                  prodIds: ProdIds
              },
            func);
    };

    this.UpdateQueueStatus = function (QueueId, QueueStatus, func) {
        AysPost("UpdateQueueStatus",
              {
                  queueId: QueueId,
                  queueStatus: QueueStatus
              },
            func);
    };

    this.UpdateQueue = function (Queue, func) {
        AysPost("UpdateQueue",
              {
                  queue: Queue
              },
            func);
    };
    this.DeleteQueue = function (Queue, func) {
        AysPost("DeleteQueue",
              {
                  queue: Queue
              },
            func);
    };

    this.ApproveOrder = function (Documents, DocumentDetails, func) {
        AysPost("ApproveOrder",
              {
                  documents: Documents,
                  documentDetails: DocumentDetails
              },
            func);
    };

    this.RejectOrder = function (Documents, DocumentDetails, func) {
       
        AysPost("UpdateDocument",
              {
                  documents: Documents,
                  documentDetails: DocumentDetails
              },
            func);
    };

    this.GetTransectionByOrderNoAndMat = function (OrderNo, MatNo, func, isExport) {
        var Export = false;
        if (isExport) {
            Export = isExport;
        }
        AysPost("GetTransectionByOrderNoAndMat",
              {
                  orderNo: OrderNo,
                  matNo: MatNo,
                  isExport: Export
              },
            func);
    };

    this.GetTransection = function (func, DateFrom, DateTo, PlantSrc, PlantDest, OrderId, ProductId, PoId, MoveType,Status, IssueBy) {
        AysPost("GetTransectionDetail",
              {
                  dateFrom: DateFrom,
                  dateTo: DateTo,
                  plantSrc: PlantSrc,
                  plantDest: PlantDest,
                  orderId: OrderId,
                  productId: ProductId,
                  poId: PoId,
                  moveType: MoveType,
                  status: Status,
                  issueBy: IssueBy
              },
            func);
    };

    this.DeleteDocument = function (OrderNo, func) {
        AysPost("DeleteDocument",
              {
                  orderNo: OrderNo
              },
            func);
    };

    this.InsertDocumentDetail = function (OrderNo, Matno, Qty, func) {
        AysPost("InsertDocumentDetail",
              {
                  orderNo: OrderNo,
                  matno: Matno,
                  qty: Qty
              },
            func);
    };
    this.UpdateDocumentDetail = function (DocumentDetail, func) {
        AysPost("UpdateDocumentDetail",
              {
                  documentDetail: DocumentDetail
              },
            func);
    };
    this.DeleteDocumentDetail = function (OrderNo , MatNo, func) {
        AysPost("DeleteDocumentDetail",
              {
                  orderNo: OrderNo,
                  matNo : MatNo
              },
            func);
    };

    this.GetConfirmOrder = function (OrderNo, MatNo, func) {
        var domain = "/service/api.ashx";
        var _data = { metdhodName: "getOrderConfirm", orderNo: OrderNo, matNo: MatNo };
        var uriPost = domain;
        jQuery.ajax({
            type: "POST",
            url: uriPost,
            async: false,
            data: _data,
            dataType: "json",
            success: function (data) {
                if (func) {
                    if (data && data.Error === '-0') {
                        window.location = '/Page/login.aspx';
                    }
                    func(data);
                }
                //if (_methodName != 'GetCurrentUserDetail') {
                //closeWaitingDialog();
                //}
            },
            error: function (data) {
                if (func) {
                    func(data);
                }
            }
        });
    };
}

this.AysPost = function (_methodName, _postData, func, async) {
    //waitingDialog({ title: "Loading...", message: "Please wait." });
    var domain = "/service/api.ashx";
    var Async = false;
    if (async == false) {
        Async = false;
    }
    var _data = { metdhodName: _methodName, postData: _postData };
    var uriPost = domain;
    jQuery.ajax({
        type: "POST",
        url: uriPost,
        async: Async,
        data: _data,
        dataType: "json",
        success: function (data) {
            if (func) {
                if (data && data.Error === '-0') {
                    window.location = '/Page/login.aspx';
                }
                func(data);
            }
            //if (_methodName != 'GetCurrentUserDetail') {
            //closeWaitingDialog();
            //}
        },
        error: function (data) {
            if (func) {
                func(data);
            }
        }
    });
};

function formatDate(val) {
    if (val === null || val === '') {
        return '-';
    }
    else {
        var d = new Date(parseInt(val.substr(6)));
        return jQuery.datepicker.formatDate('dd-MM-yy', d) + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
    }
}

function updateStatusBar(val) {
    if (val === null || val === '') {
        return null;
    }
    else {
        jQuery("#errorMessage").text(val);
        //return jQuery.datepicker.formatDate('dd-MM-yy', d) + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
    }
}
